package ua.com.infinite.mobile;

import ua.com.infinite.mobile.slidingmenu.BaseMenuFragment;
import ua.com.infinite.mobile.slidingmenu.BaseSlidingActivity;
import ua.com.infinite.mobile.slidingmenu.fragments.FragmentParentalActions;
import ua.com.infinite.mobile.slidingmenu.fragments.FragmentParentalCheckins;
import ua.com.infinite.mobile.slidingmenu.fragments.FragmentParentalNews;
import ua.com.infinite.mobile.slidingmenu.lib.SlidingMenu;
import ua.com.infinite.mobile.slidingmenu.lib.app.SlidingFragmentActivity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

public class MainActivity extends SlidingFragmentActivity {

	private Fragment mContent;
	private String lastTag = "";
	protected BaseMenuFragment baseMenuFragment;
	private boolean isMenuShowed = false;
	private SlidingMenu sm;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		setTheme(THEME);
		super.onCreate(savedInstanceState);

		sm = getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindOffsetRes(R.dimen.actionbar_home_width);
		
		actionBar = getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);

		if (savedInstanceState != null) {
			lastTag = savedInstanceState.getString("tag");
			mContent = getSupportFragmentManager().getFragment(savedInstanceState, lastTag);
			//mContent = getSupportFragmentManager().findFragmentByTag(lastTag);
			isMenuShowed = savedInstanceState.getBoolean("isMenuShow");
		}
		if (mContent == null) {
			showMenuAction("News");

		}
		if(isMenuShowed){
			sm.showBehind();
		}

		baseMenuFragment = BaseMenuFragment.newInstance();

		setContentView(R.layout.content_frame);
		getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mContent).commit();

		setBehindContentView(R.layout.menu_frame);
		getSupportFragmentManager().beginTransaction().replace(R.id.menu_frame, baseMenuFragment).commit();

		setSlidingActionBarEnabled(true);
		baseMenuFragment.setMain(this);

		

		if (Build.VERSION.SDK_INT >= 11) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString("tag", lastTag);
		if (lastTag.equals("")) {
			getSupportFragmentManager().putFragment(outState, "News", mContent);
		} else {
			getSupportFragmentManager().putFragment(outState, lastTag, mContent);
		}
		outState.putBoolean("isMenuShow", sm.isBehindShowing());
		getSupportFragmentManager().putFragment(outState, "menu", baseMenuFragment);

	}

	public BaseMenuFragment getBaseMenuFragment() {
		return (BaseMenuFragment) this.baseMenuFragment;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			this.toggle();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	public void switchContent(Fragment fragment) {
		FragmentManager man = getSupportFragmentManager();
		mContent = fragment;
		getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
		toggle();
		getSlidingMenu().showAbove();
	}

	public void showMenuAction(String tag) {
		if (lastTag.equals(tag)) {
			toggle();
			return;
		} else {
			lastTag = tag;
		}
		Fragment preContent = null;
		FragmentManager man = getSupportFragmentManager();
		if ((preContent = getSupportFragmentManager().findFragmentByTag(tag)) != null) {
			switchContent(preContent);
			actionBar.setTitle(tag);
			toggle();
			return;
		}
		if (tag.equals("News")) {
			preContent = FragmentParentalNews.newInstance(getSlidingMenu());
			switchContent(preContent);

		}
		if (tag.equals("CheckIn")) {
			preContent = FragmentParentalCheckins.newInstance(getSlidingMenu());
			switchContent(preContent);

		}
		if (tag.equals("Actions")) {
			preContent = FragmentParentalActions.newInstance(getSlidingMenu());
			
			switchContent(preContent);
		}
		//getSupportFragmentManager().beginTransaction().add(preContent, tag).commit();
		actionBar.setTitle(tag);
	}
	
	
	
	

}
